#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>

#define SERVER_IP "127.0.0.1"
#define SERVER_PORT 8000
#define MAX_BUFFER_SIZE 1024

int main()
{
    // 创建 socket
    int client_sock = socket(AF_INET, SOCK_STREAM, 0);

    // 设置服务器地址
    struct sockaddr_in server_addr;
    memset(&server_addr, 0, sizeof(server_addr));
    server_addr.sin_family = AF_INET;
    server_addr.sin_addr.s_addr = inet_addr(SERVER_IP);
    server_addr.sin_port = htons(SERVER_PORT);

    // 连接服务器
    if (connect(client_sock, (struct sockaddr *)&server_addr, sizeof(server_addr)) == -1)
    {
        perror("Connect error");
        exit(-1);
    }

    // 发送请求
    char request[MAX_BUFFER_SIZE] = {0};
    char response[MAX_BUFFER_SIZE] = {0};
    sprintf(request, "OPTIONS DianTP://%s:%d DianTP/0.5\r\nCSeq: 1\r\n\r\n", SERVER_IP, SERVER_PORT);
    send(client_sock, request, strlen(request), 0);
    printf("Sent OPTIONS request\n");
    sleep(1);//让server有足够时间处理数据包，防止粘包问题

    sprintf(request, "SETUP DianTP://%s:%d DianTP/0.5\r\nCSeq: 2\r\nTransport: RTP/UDP;client_port=4588-4589\r\n\r\n", SERVER_IP, SERVER_PORT);
    send(client_sock, request, strlen(request), 0);
    printf("Sent SETUP request\n");
    sleep(1);

    sprintf(request, "PLAY DianTP://%s:%d DianTP/0.5\r\nCSeq: 3\r\nSession: 123456\r\nRange: npt=0-\r\n\r\n", SERVER_IP, SERVER_PORT);
    send(client_sock, request, strlen(request), 0);
    printf("Sent PLAY request\n");
    sleep(1);

    sprintf(request, "TEARDOWN DianTP://%s:%d DianTP/0.5\r\nCSeq: 4\r\nSession: 123456\r\n\r\n", SERVER_IP, SERVER_PORT);
    send(client_sock, request, strlen(request), 0);
    printf("Sent TEARDOWN request\n");
    sleep(1);

    // 关闭连接
    close(client_sock);

    return 0;
}
